define (require, exports, module) ->
  # 引入服务
  authApi = require 'mods/auth/auth_api'

  module.exports =
    # 检查是否登录
    checkLogin: (to, from, next) ->
      # 获取登录用户信息
      authApi.getUserSession()
        .then (res) ->
          if 200 is res?.status and 'true' is '' + res?.data?.anonymous
            next
              path: '/login'
              query:
                redirect:  to.fullPath
          # else if 405 is res?.status and 'true' is '' + res?.data?.anonymous
          #   next
          #     path: '/trade_pwd'
          #     query:
          #       redirect:  to.fullPath
          else
            return next()
        .catch (err) ->
          if 403 is err?.status
            next
              path: '/login'
              query:
                redirect:  to.fullPath
          else
            console.error err
            return next false

    # 退出登录
    logOut: (to, from, next) ->
      authApi.doLoginOut()
        .then (res) ->
          return next '/'
        .catch (err) ->
          console.error err
          return next false